package gl.android.biyiqing;

import gl.android.biyiqing.bean.BYQUserInfo;
import gl.android.biyiqing.competent.ZEYOUCallBack;
import gl.android.biyiqing.fragment.UserInfoFragment;
import gl.uitls.AndroidUtil;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class SearchOptionActivity extends BYQBaseFragemtActivity implements
		OnClickListener {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_option);
		bindViews();
	}

	private LinearLayout activity_regist_scroll;
	private LinearLayout content;
	private TextView regist_email;
	private TextView regist_phone;
	private TextView regist_code;
	private TextView regist_pass;
	private TextView regist_nick;
	private TextView hascar;
	private TextView shouruq;
	private TextView shourus;
	private RadioButton radio0;
	private RadioButton radio1;
	private TextView regist_complete_submit_btn;
	private TextView hyqk;
	private TextView juzhuqingk;

	// End Of Content View Elements

	private void bindViews() {

		activity_regist_scroll = (LinearLayout) findViewById(R.id.activity_regist_scroll);
		content = (LinearLayout) findViewById(R.id.content);
		regist_email = (TextView) findViewById(R.id.regist_email);
		regist_email.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				final String message = "请输入年龄";
//				final String title = "起始年龄";
//				genIntInputDialog(message, title, regist_email);
				UserInfoFragment.genIntInputDialog2(new String[] { "最小年龄",
				"最大年龄" }, "请输入一个整数", regist_email,
				new ZEYOUCallBack() {

					@Override
					public void callback(TextView regist_email2) {
						final Object tag2 = regist_email2.getTag();
						regist_email.setText(tag2.toString().replace(",", "-"));
						regist_email.setTag(tag2.toString().split(",")[0]);
						regist_phone.setTag(tag2.toString().split(",")[1]);
					}
				});
			}

		});
		regist_phone = (TextView) findViewById(R.id.regist_phone);
		regist_phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final String message = "请输入年龄";
				final String title = "结束年龄";
				genIntInputDialog(message, title, regist_phone);
			}

		});
		regist_code = (TextView) findViewById(R.id.regist_code);
		regist_code.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				final String message = "请输入身高(cm/厘米)";
//				final String title = "起始身高";
//				genIntInputDialog(message, title, regist_code);
				UserInfoFragment.genIntInputDialog2(new String[] { "最小身高",
				"最大身高" }, "请输入一个整数", regist_code,
				new ZEYOUCallBack() {

					@Override
					public void callback(TextView regist_email2) {
						final Object tag2 = regist_email2.getTag();
						regist_code.setText(tag2.toString().replace(",", "-"));
						regist_code.setTag(tag2.toString().split(",")[0]);
						regist_pass.setTag(tag2.toString().split(",")[1]);
					}
				});
			}

		});
		regist_pass = (TextView) findViewById(R.id.regist_pass);
		regist_pass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final String message = "请输入身高(cm/厘米)";
				final String title = "结束身高";
				genIntInputDialog(message, title, regist_pass);
			}
		});
		regist_nick = (TextView) findViewById(R.id.regist_nick);
		regist_nick.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				genRaidoListDialog(BYQUserInfo.getReadableArray(BYQUserInfo.XUELI),regist_nick);
			}
		});
		hascar = (TextView) findViewById(R.id.hascar);
		hascar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				genRaidoListDialog(BYQUserInfo.getReadableArray(BYQUserInfo.XUELI),hascar);
			}
		});
		shouruq = (TextView) findViewById(R.id.shouruq);
		shouruq.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				genRaidoListDialog(BYQUserInfo.getReadableArray(BYQUserInfo.SHOURU),shouruq);
			}
		});

		shourus = (TextView) findViewById(R.id.shourus);
		shourus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				genRaidoListDialog(BYQUserInfo.getReadableArray(BYQUserInfo.SHOURU),shourus);
			}
		});
		juzhuqingk = (TextView) findViewById(R.id.juzhuqingk);
		juzhuqingk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				genRaidoListDialog(BYQUserInfo.getReadableArray(BYQUserInfo.JUZHUQINGKUANG),juzhuqingk);
			}
		});

		hyqk = (TextView) findViewById(R.id.hyqk);
		hyqk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				genRaidoListDialog(BYQUserInfo.getReadableArray(BYQUserInfo.HUNYINZHUNGKUANG),hyqk);
			}
		});

		radio0 = (RadioButton) findViewById(R.id.radio0);
		radio1 = (RadioButton) findViewById(R.id.radio1);
		regist_complete_submit_btn = (TextView) findViewById(R.id.regist_complete_submit_btn);
		regist_complete_submit_btn.setOnClickListener(this);
	}

	
	

	protected void genRaidoListDialog(final String[] readableArray,
			final TextView regist_nick2) {
		final ListView listview = new ListView(this);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				this, R.layout.item_list_xingqu,
				android.R.id.text1);
		int i = 0;
		for (String s : readableArray) {
			if (i != 0) {
				adapter.add(s);
			}
			i++;
		}
		final AlertDialog dialog = AndroidUtil.genCustomViewDialog(this, listview).show();;
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent,
					View view, int position, long id) {
				regist_nick2.setTag(position+1);
				regist_nick2.setText(readableArray[position+1]);
				dialog.dismiss();
			}
		});
		
	
		
	}

	public void genIntInputDialog(final String message, final String title,
			final TextView regist_email2) {
		final EditText et = new EditText(this);
		et.setInputType(InputType.TYPE_CLASS_NUMBER);
		//设置最大输入字符为3
		et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
		AndroidUtil.genCustomViewDialog(this, et, message, title,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						try {
							if (et.getText() != null) {

								regist_email2.setTag(et.getText().toString());
								regist_email2.setText(et.getText().toString());
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				});
	}

	public String getValue(final View regist_email2, final String age) {
		try {
			return ""+Integer
					.parseInt(regist_email2.getTag().toString());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return "0";
		} catch (Exception e) {
			if (regist_email2.getTag()!=null) {
				return regist_email2.getTag().toString();
			}
		}
		return "0";
	}
	private void setResult() {
		// data=起始年龄，结束年龄，起始身高，结束身高 ，起始学历，结束学历，起始收入，结束收入，性别
		String data = String.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
				getValue(regist_email, "年龄"), getValue(regist_phone, "年龄"),
				getValue(regist_code, "身高"), getValue(regist_pass, "身高"),
				getValue(regist_nick, BYQUserInfo.XUELI), getValue(hascar, BYQUserInfo.XUELI),
				getValue(shouruq, BYQUserInfo.SHOURU), getValue(shourus, BYQUserInfo.SHOURU),
				radio0.isChecked() ? "1" : 2,getValue(juzhuqingk, BYQUserInfo.JUZHUQINGKUANG),getValue(hyqk, BYQUserInfo.HUNYINZHUNGKUANG));
		getIntent().putExtra("data", data);
		System.out.println(data);
		setResult(RESULT_OK, getIntent());
	}
	private void goSearchActivity(String type, String data) {
		Intent intent = new Intent(this, SearchActivity.class);
		Bundle extras = new Bundle();
		extras.putString("type", type);
		extras.putString("data", data);
		intent.putExtras(extras);
		startActivity(intent);
	}
	@Override
	public void onClick(View v) {
		setResult();
		goSearchActivity("1", getIntent().getExtras().getString("data"));
	}
}
